import json

from django.http import HttpResponse
from django.shortcuts import render
from user.models import User
from user.service import UserService


# Create your views here.
def index(request):
        return render(request, 'user/index.html')

def login_controller(request):
        """
    登录view
    :param request:
    :return: html
    """
        user_name = request.POST.get("userName")
        password = request.POST.get("password")
        if user_name is None or password is None:
            print("用户名或者密码为空")
            return render(request, 'user/login.html')
        else:
            judgment = UserService().login_service(user_name, password)
            if judgment is True:
                request.session["login" + user_name] = user_name
                return render(request, 'user/index.html')
            else:
                return render(request, 'user/login.html')

def register_controller(request):
        """
    注册view
    :param request:
    :return: html
    """
        user_name = request.POST.get("userName")
        password = request.POST.get("password")
        question = request.POST.get("question")
        answer = request.POST.get("answer")
        if (user_name is None) or password is None or question is None or answer is None:
            print("有一项为空")
            return render(request, 'user/register.html')
        else:
            judgment = UserService().register_service(user_name, password, question, answer)
            if judgment is True:
                return render(request, 'user/index.html')
            else:
                return render(request, 'user/register.html')

def login(request):
        '''
           retrieve:
               Return a user instance.

           list:
               Return all users,ordered by most recent joined.

           create:
               Create a new user.

           delete:
               Remove a existing user.

           partial_update:
               Update one or more fields on a existing user.

           update:
               Update a user.
       '''
        return render(request, "user/login.html")

def register(request):
        return render(request, "user/register.html")
